package com.daomx.gateway.filter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Map;

/**
 * Created by xiaodao
 * date: 2019/8/12
 * 全局过滤器
 */
//@Component
public class AuthFilter implements GlobalFilter, Ordered {
	@Override
	public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
		String token = exchange.getRequest().getHeaders().getFirst("token");
		if (token == null || token.isEmpty()) {
			ServerHttpResponse response = exchange.getResponse();

			// 封装错误信息
			Map<String, Object> responseData = Maps.newHashMap();
			responseData.put("code", 401);
			responseData.put("message", "非法请求");
			responseData.put("cause", "Token is empty");

			try {
				// 将信息转换为 JSON
				ObjectMapper objectMapper = new ObjectMapper();
				byte[] data = objectMapper.writeValueAsBytes(responseData);

				// 输出错误信息到页面
				DataBuffer buffer = response.bufferFactory().wrap(data);
				response.setStatusCode(HttpStatus.UNAUTHORIZED);
				response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
				return response.writeWith(Mono.just(buffer));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}

		return chain.filter(exchange);
	}

	/***
	 * 越小优先级越高
	 * @return
	 */
	@Override
	public int getOrder() {
		return 0;
	}
}
